home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / kernel / lfs / lfsIo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-19  |  5.0 KB  |  185 lines

  1. /* 
  2.  * lfsSegIo.c --
  3.  *
  4.  *    Read and write bytes from LFS segments.
  5.  *
  6.  * Copyright 1989 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /cdrom/src/kernel/Cvsroot/kernel/lfs/lfsIo.c,v 1.8 92/03/19 17:31:56 jhh Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #include <sprite.h>
  21. #include <lfs.h>
  22. #include <lfsInt.h>
  23. #include <dev.h>
  24. #include <fs.h>
  25. #include <devFsOpTable.h>
  26.  
  27.  
  28. /*
  29.  *----------------------------------------------------------------------
  30.  *
  31.  * LfsReadBytes --
  32.  *
  33.  *    Read bytes from an lfs disk.
  34.  *
  35.  * Results:
  36.  *    Error status.
  37.  *
  38.  * Side effects:
  39.  *    None.
  40.  *
  41.  *----------------------------------------------------------------------
  42.  */
  43.  
  44. ReturnStatus
  45. LfsReadBytes(lfsPtr, diskAddress, numBytes, bufferPtr)
  46.     Lfs        *lfsPtr;    /* Target file system. */
  47.     LfsDiskAddr diskAddress;    /* Disk address to read from. */
  48.     int        numBytes;    /* Number of bytes to read. */
  49.     char    *bufferPtr;    /* Buffer to read into. */
  50. {
  51.     struct args { 
  52.     Fs_IOParam    readParams;
  53.     Fs_IOReply    reply;
  54.     } args;
  55.     ReturnStatus status;
  56.     char    smallBuffer[DEV_BYTES_PER_SECTOR];
  57.     int    offset;
  58.  
  59.     /*
  60.      * Check in the seg cache.
  61.      */
  62.     if (lfsPtr->segCache.valid) {
  63.     if (LfsDiskAddrInRange(diskAddress, LfsBytesToBlocks(lfsPtr, numBytes),
  64.                 lfsPtr->segCache.startDiskAddress,
  65.                 lfsPtr->segCache.endDiskAddress)) {
  66.         offset = LfsDiskAddrOffset(diskAddress, 
  67.             lfsPtr->segCache.startDiskAddress);
  68.         offset = LfsBlocksToBytes(lfsPtr, offset);
  69.         bcopy(lfsPtr->segCache.memPtr + offset, bufferPtr, numBytes);
  70.         LFS_STATS_INC(lfsPtr->stats.blockio.segCacheHits);
  71.         return SUCCESS;
  72.     }
  73.     }
  74.  
  75.  
  76.     bzero((char *)&args, sizeof(args));
  77.  
  78.     if (numBytes < DEV_BYTES_PER_SECTOR) { 
  79.     args.readParams.buffer = smallBuffer;
  80.     args.readParams.length = DEV_BYTES_PER_SECTOR;
  81.     } else {
  82.     args.readParams.buffer = bufferPtr;
  83.     args.readParams.length = numBytes;
  84.     }
  85.     offset = LfsDiskAddrToOffset(diskAddress);
  86.     args.readParams.offset = offset * DEV_BYTES_PER_SECTOR;
  87.     args.readParams.flags = 
  88.     args.readParams.procID = 0;
  89.     args.readParams.familyID = 0;
  90.     args.readParams.uid = 0;
  91.     args.readParams.reserved = 0;
  92.     status = (*devFsOpTable[DEV_TYPE_INDEX(lfsPtr->devicePtr->type)].read)
  93.         (lfsPtr->devicePtr, &args.readParams, &args.reply);
  94.     if (status != SUCCESS) {
  95.     LfsError(lfsPtr, status, "LfsReadBytes failed");
  96.     }
  97.     if (numBytes < DEV_BYTES_PER_SECTOR) {
  98.     if (args.reply.length != DEV_BYTES_PER_SECTOR) {
  99.         /*
  100.          * Short read. 
  101.          */
  102.         return FAILURE;
  103.     }
  104.     bcopy(smallBuffer, bufferPtr, numBytes);
  105.     } else {
  106.     if (args.reply.length != numBytes) {
  107.         /*
  108.          * Short read. 
  109.          */
  110.         return FAILURE;
  111.     }
  112.     }
  113.     LFS_STATS_ADD(lfsPtr->stats.blockio.totalBytesRead, numBytes);
  114.     return status;
  115. }
  116.  
  117. /*
  118.  *----------------------------------------------------------------------
  119.  *
  120.  * LfsWriteBytes --
  121.  *
  122.  *    Write bytes from an lfs disk.
  123.  *
  124.  * Results:
  125.  *    Error status.
  126.  *
  127.  * Side effects:
  128.  *    None.
  129.  *
  130.  *----------------------------------------------------------------------
  131.  */
  132.  
  133. ReturnStatus
  134. LfsWriteBytes(lfsPtr, diskAddress, numBytes, bufferPtr)
  135.     Lfs        *lfsPtr;    /* Target file system. */
  136.     LfsDiskAddr diskAddress;    /* Disk address to send data. */
  137.     int        numBytes;    /* Number of bytes to write. */
  138.     char    *bufferPtr;    /* Buffer to write into. */
  139. {
  140.     struct args { 
  141.     Fs_IOParam    writeParams;
  142.     Fs_IOReply    reply;
  143.     } args;
  144.     char    smallBuffer[DEV_BYTES_PER_SECTOR];
  145.     ReturnStatus    status;
  146.     int    offset;
  147.  
  148.  
  149.     offset = LfsDiskAddrToOffset(diskAddress);
  150.     bzero((char *)&args, sizeof(args));
  151.  
  152.     if (numBytes < DEV_BYTES_PER_SECTOR) { 
  153.     args.writeParams.buffer = smallBuffer;
  154.     args.writeParams.length = DEV_BYTES_PER_SECTOR;
  155.     bzero(smallBuffer + numBytes, DEV_BYTES_PER_SECTOR-numBytes);
  156.     bcopy(bufferPtr, smallBuffer, numBytes);
  157.     } else {
  158.     args.writeParams.buffer = bufferPtr;
  159.     args.writeParams.length = numBytes;
  160.     }
  161.     args.writeParams.offset = offset * DEV_BYTES_PER_SECTOR;
  162.     args.writeParams.flags = 
  163.     args.writeParams.procID = 0;
  164.     args.writeParams.familyID = 0;
  165.     args.writeParams.uid = 0;
  166.     args.writeParams.reserved = 0;
  167.     status = (*devFsOpTable[DEV_TYPE_INDEX(lfsPtr->devicePtr->type)].write)
  168.         (lfsPtr->devicePtr, &args.writeParams, &args.reply);
  169.     if (status != SUCCESS) {
  170.     LfsError(lfsPtr, status, "LfsWriteBytes");
  171.     }
  172.     if (numBytes < DEV_BYTES_PER_SECTOR) {
  173.     if (args.reply.length != DEV_BYTES_PER_SECTOR) {
  174.         LfsError(lfsPtr, FAILURE, "LfsWriteBytes short write");
  175.     }
  176.     } else {
  177.     if (args.reply.length != numBytes) {
  178.         LfsError(lfsPtr, FAILURE, "LfsWriteBytes short write");
  179.     }
  180.     }
  181.     LFS_STATS_ADD(lfsPtr->stats.blockio.totalBytesWritten, numBytes);
  182.     return status;
  183. }
  184.  
  185.